package com.colorful.insuranceproject.request;

import android.os.Handler;

import com.lovely3x.common.managements.user.TokenManager;
import com.lovely3x.common.requests.impl.HandlerRequestImpl;
import com.lovely3x.common.utils.Response;
import com.lovely3x.jsonparser.Config;
import com.lovely3x.jsonparser.model.JSONKey;
import com.lovely3x.jsonparser.model.JSONKeyImpl;
import com.lovely3x.jsonparser.model.JSONObject;
import com.lovely3x.jsonparser.model.JSONValue;
import com.lovely3x.jsonparser.model.JSONValueImpl;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by lovely3x on 16-5-24.
 */
public class BaseRequest extends HandlerRequestImpl {

    /**
     * 传递json数据时使用的key
     */
    public static final String JSON_PARAMETER_KEY = "json";

    public BaseRequest(Handler handler) {
        super(handler);
    }

    /**
     * 将参数包装为JSON串传递给服务器
     *
     * @param pr
     * @param action
     * @param what
     * @param params
     */
    public void processWithJSON(ProcessResult pr, String action, int what, Object... params) {
        process(pr, action, what, JSON_PARAMETER_KEY, wrapParameterToJSON(params).toString());
    }

    /**
     * 将参数包装为JSON串传递给服务器
     *
     * @param pr
     * @param action
     * @param what
     * @param params
     */
    public void processWithJSON(ProcessResultExtension pr, String action, int what, Object... params) {
        process(pr, action, what, JSON_PARAMETER_KEY, wrapParameterToJSON(params).toString());
    }

    public static JSONObject wrapParameterToJSON(Object... params) {
        final HashMap<String, Object> map = new HashMap<>();

        if (params != null) {
            if (params.length % 2 == 0) {
                for (int i = 0; i < params.length; i += 2) {
                    String key = String.valueOf(params[i]);
                    Object object = params[i + 1];
                    map.put(key, object);
                }
                wrapToken(map);
            } else {
                throw new IllegalArgumentException("Parameter incorrect !");
            }
        }
        return new JSONObject(map);
    }

    /**
     * 包装token进map
     *
     * @param map
     */
    public static void wrapToken(Map<String, Object> map) {
        String token = TokenManager.getInstance().getTokeValue();
        map.put(TOKEN_KEY, token == null ? INVALID_TOKEN : token);
    }




    /**
     * 包装token进map
     *
     * @param map
     */
    public static void wrapJSONToken(Map<JSONKey, JSONValue> map) {
        String token = TokenManager.getInstance().getTokeValue();
        map.put(new JSONKeyImpl(Config.createDefault(), TOKEN_KEY), new JSONValueImpl(Config.createDefault(), token == null ? INVALID_TOKEN : token));
    }


    /**
     * @param action
     * @param what
     * @param params
     * @return
     */
    public Response processSyncWithJSON(String action, int what, Object... params) {
        return processSync(action, what, JSON_PARAMETER_KEY, wrapParameterToJSON(params).toString());
    }

}
